import { useOperationConfiguration } from '@renderer/stores/operationConfiguration'
import { storeToRefs } from 'pinia'
import { createWebHistory, createRouter } from 'vue-router'

const routes = [
  { path: '/', component: () => import('@renderer/pages/index.vue') },
  { path: '/login', name: 'login', component: () => import('@renderer/pages/login.vue') }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to) => {
  const operationConfigurationStore = useOperationConfiguration()
  const { token } = storeToRefs(operationConfigurationStore)
  if (token.value || to.name === 'login') {
    return true
  } else {
    return { name: 'login', replace: true }
  }
})

export default router
